package com.platform.mall.controller;

import cn.hutool.core.map.MapUtil;
import com.platform.mall.common.annotation.AnonymousAccess;
import com.platform.mall.common.constants.SystemConstants;
import com.platform.mall.common.result.Result;
import com.platform.mall.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Slf4j
@Api(tags = "认证中心")
@RestController
@RequestMapping("/auth")
@AllArgsConstructor
public class AuthController {

    private final AuthService authService;

    @ApiOperation(value = "security认证", notes = "登录入口")
    @AnonymousAccess
    @PostMapping("/login")
    public Result postAccessToken(@RequestBody Map<String, String> model, HttpServletRequest request) {
        String userType = MapUtil.getStr(model, SystemConstants.USER_TYPE);
        String grantType = MapUtil.getStr(model, SystemConstants.GRANT_TYPE);
        Assert.notNull(userType, "用户类型不能为空！");
        Assert.notNull(grantType, "授权类型不能为空！");
        return authService.login(model);
    }

    @ApiOperation(value = "注销")
    @AnonymousAccess
    @DeleteMapping("/logout")
    public Result logout() {
        return authService.logout();
    }

}
